In awk, $0
represents the entire current line being processed. Its behavior depends on how it’s used:
As a Pattern:
When used as a pattern (the condition in an awk script), $0
acts as a truthy/falsy test for whether the current line is non-empty.
- True (prints the line): If the line contains any text.
- False (skips the line): If the line is empty (contains only whitespace or nothing).
Example:
The command awk '$0' filename.txt
effectively prints all non-empty lines from filename.txt
. This is because the script implicitly prints the current line ($0
) if the pattern $0
evaluates to true (i.e., the line is not empty).
Illustrative Example:
Let’s say filename.txt
contains:
Hello
World
The output of awk '$0' filename.txt
would be:
Hello
World
The blank line is omitted because $0
is false for that line.
Summary:
$0
refers to the entire current line.- As a pattern,
$0
checks if the line is non-empty. If true, the line is typically printed (due to awk’s default action). awk '$0'
provides a concise way to print only non-empty lines from a file.